import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        token: uni.getStorageSync('token'),
        userInfo: {
            userId: null,
            id: null,
        }, // 用户信息
        deviceInfo: {},
        ble: { // 蓝牙全局变量
            isOpenBleAdapter: false, // 是否初始化蓝牙适配器
            devID: null, // 当前连接的仪器deviceId
            BleConnectedName: null, // 当前要连接仪器名称
            state: -1, // 蓝牙状态 -5:连接超时  -4:连接失败  -3:连接中  -2:搜索中  -1:未连接  0:已连接  1: 使用中
            measureState: 0, // 蓝牙测量状态  -3:待机中 -2:仪器准备中  -1:仪器准备完成  0:未连接  1:测量中 2:测量结束 3:上传成功 4:上传失败  5:上传中
            defaultInfo: {
                version: '',
                battery: '',
                mac: '',
                name: ''
            },
            versionInfo: {
                FwId: '',
                HwVersion: '',
                FwVersion: '',
                aVersion: ''
            }
        },
    },
    getters: {
        getUserInfo (state) {
            let userInfo = JSON.parse(uni.getStorageSync('app-userInfo') || '{}')
            return userInfo&&userInfo.id ? userInfo : state.userInfo
        },
        getDeviceInfo (state) {
            let deviceInfo = JSON.parse(uni.getStorageSync('app-deviceInfo') || '{}')
            return deviceInfo.deviceId ? deviceInfo : state.deviceInfo
        },
        getBle (state) {
            return state.ble
        },
    },
    mutations: {
        // 保存token
        saveToken (state, PARAMS) {
            state.token = PARAMS
        },
        setUserInfo (state, PARAMS) {
            uni.setStorageSync('app-userInfo', JSON.stringify(PARAMS))
            state.userInfo = PARAMS
        },
        // 设置当前账号绑定的仪器信息
        setDeviceInfo (state, PARAMS) {
            uni.setStorageSync('app-deviceInfo', JSON.stringify(PARAMS))
            state.deviceInfo = PARAMS
        },
        // 改变蓝牙适配器可用状态
        changeIsOpenBleAdapter (state, bol) {
            state.ble.isOpenBleAdapter = bol
        },
        // 设置当前进行搜索的蓝牙名称
        setBleConnectedName (state, PARAMS) {
            state.ble.BleConnectedName = PARAMS
        },
        // 重置当前进行搜索的蓝牙名称
        resetBleConnectedName (state) {
            state.ble.BleConnectedName = null
        },
        // 设置蓝牙基础信息 
        setDefaultInfo (state, PARAMS) {
            if (PARAMS.type == 1) {
                state.ble.defaultInfo.mac = PARAMS.data.mac
                state.ble.defaultInfo.name = PARAMS.data.name
            } else {
                state.ble.state = 0
                state.ble.defaultInfo.battery = PARAMS.data.battery
                state.ble.defaultInfo.version = PARAMS.data.version
            }

        },
        // 设置蓝牙仪器所用版本信息
        setVersionInfo (state, PARAMS) {
            state.ble.versionInfo.FwId = PARAMS.FwId
            state.ble.versionInfo.HwVersion = PARAMS.HwVersion
            state.ble.versionInfo.FwVersion = PARAMS.FwVersion
            state.ble.versionInfo.aVersion = PARAMS.aVersion
        },
        // 设置当前连接的仪器deviceId
        setDevId (state, PARAMS) {
            state.ble.devID = PARAMS
        },
        // 变动此时蓝牙连接的状态
        changeBleState (state, PARAMS) {
            state.ble.state = PARAMS
        },
        // 变动此时蓝牙进行操作的状态
        changeBleMeasureState (state, PARAMS) {
            console.log('changeBleMeasureState', PARAMS);
            state.ble.measureState = PARAMS
        },
        // 重置连接所有状态
        reset (state, PARAMS) {
            state.ble.devID = null
            state.ble.BleConnectedName = null
            state.ble.state = -1
            state.ble.measureState = 0
            state.ble.defaultInfo = {
                version: '',
                battery: '',
                mac: '',
                name: ''
            }
            state.ble.versionInfo = {
                FwId: undefined,
                HwVersion: '',
                FwVersion: '',
                aVersion: ''
            }
        },
        resetUserInfo (state) {
            state.userInfo = {}
        },
        resetDeviceInfo (state) {
            state.deviceInfo = {}
        }
    }
})

export default store